    //
//  RootViewController_iPad.m
//  SecretFolder
//
//  Created by Deepak Kumar on 23/06/12.
//  Copyright 2012 Frog. All rights reserved.
//

#import "RootViewController_iPad.h"
#import "HomeViewController_iPad.h"
#import "DrawPatternLockViewController.h"
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#import "AVCamCaptureManager.h"
#import "AVCamRecorder.h"
#import <AVFoundation/AVFoundation.h>

static void *AVCamFocusModeObserverContext = &AVCamFocusModeObserverContext;

@implementation RootViewController_iPad

@synthesize retryCount;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    NSDictionary *size = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Helvetica" size:19],UITextAttributeFont, nil];
    self.navigationController.navigationBar.titleTextAttributes = size;
    if ([self captureManager] == nil) {
		AVCamCaptureManager *manager = [[AVCamCaptureManager alloc] init];
		[self setCaptureManager:manager];
		
		[[self captureManager] setDelegate:self];
        
		if ([[self captureManager] setupSession]) {
            // Create video preview layer and add it to the UI
			AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[[self captureManager] session]];
			UIView *view = [self videoPreviewView];
			CALayer *viewLayer = [view layer];
			[viewLayer setMasksToBounds:YES];
			
			CGRect bounds = [view bounds];
			[newCaptureVideoPreviewLayer setFrame:bounds];
			
            //			if ([newCaptureVideoPreviewLayer isOrientationSupported]) {
            //				[newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait];
            //			}
			
			[newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
			
            // Start the session. This is done asychronously since -startRunning doesn't return until the session is running.
			dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
				[[[self captureManager] session] startRunning];
			});
			
        }}
    retryCount = 0;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:kPasswordSaved]) {
      
    if ([[NSUserDefaults standardUserDefaults]objectForKey:kPattern]) {
        DrawPatternLockViewController *tController=[DrawPatternLockViewController new];
        [tController setTarget:self withAction:@selector(checkKey:)];
        [self.navigationController pushViewController:tController animated:YES];
    }
    else if ([[NSUserDefaults standardUserDefaults] objectForKey:kPinCode])
    {
        if (IS_IPAD) {
            HomeViewController_iPad *tController = [[HomeViewController_iPad alloc] initWithNibName:@"HomeViewController_iPad" bundle:nil];
            [self.navigationController pushViewController:tController animated:YES];
        }
        else
        {
            HomeViewController_iPad *tController = [[HomeViewController_iPad alloc] initWithNibName:@"HomeViewController_iphone" bundle:nil];
            [self.navigationController pushViewController:tController animated:YES];
        }
        
        
        [self pushCheckPinPasswordAlertView];
    }
    }
    else
    {
        if (IS_IPAD) {
            HomeViewController_iPad *tController = [[HomeViewController_iPad alloc] initWithNibName:@"HomeViewController_iPad" bundle:nil];
            [self.navigationController pushViewController:tController animated:YES];
        }
        else
        {
        HomeViewController_iPad *tController = [[HomeViewController_iPad alloc] initWithNibName:@"HomeViewController_iphone" bundle:nil];
        [self.navigationController pushViewController:tController animated:YES];
        }
    }
   

}
-(void)pushCheckPinPasswordAlertView
{
    UIAlertView *tCheckPinPasswordAlertview=[[UIAlertView alloc]initWithTitle:@"Enter Password " message:@"Enter Pin Password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    [tCheckPinPasswordAlertview setAlertViewStyle:UIAlertViewStyleSecureTextInput];
    UITextField *tPasswordTextField = [tCheckPinPasswordAlertview textFieldAtIndex:0];
    tPasswordTextField.keyboardType = UIKeyboardTypeNumberPad;
    tCheckPinPasswordAlertview.tag=1;
    [tCheckPinPasswordAlertview show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==1) {
        if([[alertView textFieldAtIndex:0].text isEqual:[[NSUserDefaults standardUserDefaults] objectForKey:kPasswordText] ])
        {
            NSLog(@"-------pin password %@",[[NSUserDefaults standardUserDefaults] objectForKey:kPasswordText]);
//            HomeViewController_iPad *tController = [[HomeViewController_iPad alloc] initWithNibName:@"HomeViewController_iPad" bundle:nil];
//            [self.navigationController pushViewController:tController animated:YES];
        }
        else if([[alertView buttonTitleAtIndex:buttonIndex] isEqual:@"Cancel"])
        {
            [self pushCheckPinPasswordAlertView];
            return;
        }
        else
        {
            UIAlertView *wrongPasswordAlertView=[[UIAlertView alloc]initWithTitle:@"Wrong Password" message:@"You have enterd wrong Password" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Retry", nil];
            wrongPasswordAlertView.tag=3;
            [wrongPasswordAlertView show];
        }
        
    }
    if([[alertView buttonTitleAtIndex:buttonIndex] isEqual:@"Retry"])
    {
        retryCount++;
        if (retryCount >= 3) {
            retryCount = 0;
            [self captureImageAndLocation];
        }

        if (alertView.tag == 3) {
            [self pushCheckPinPasswordAlertView];
            return;
        }
        
    }
}
-(BOOL)captureImageAndLocation
{
    [[self captureManager] captureStillImage];
    [[[self captureManager] session] stopRunning];
    return YES;
}
-(void)checkKey:(NSString *)key1
{
    //[[NSUserDefaults standardUserDefaults] synchronize];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:kPattern ] isEqual:key1]) {
        // [self.navigationController popViewControllerAnimated:YES];
        if (IS_IPAD) {
            HomeViewController_iPad *tController = [[HomeViewController_iPad alloc] initWithNibName:@"HomeViewController_iPad" bundle:nil];
            [self.navigationController pushViewController:tController animated:YES];
        }
        else
        {
            HomeViewController_iPad *tController = [[HomeViewController_iPad alloc] initWithNibName:@"HomeViewController_iphone" bundle:nil];
            [self.navigationController pushViewController:tController animated:YES];
        }

    }
    else if([[[NSUserDefaults standardUserDefaults] objectForKey:kDecoyPassword] isEqual:key1])
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kDecoyPasswordEntered];
        if (IS_IPAD) {
            HomeViewController_iPad *tController = [[HomeViewController_iPad alloc] initWithNibName:@"HomeViewController_iPad" bundle:nil];
            [self.navigationController pushViewController:tController animated:YES];
        }
        else
        {
            HomeViewController_iPad *tController = [[HomeViewController_iPad alloc] initWithNibName:@"HomeViewController_iphone" bundle:nil];
            [self.navigationController pushViewController:tController animated:YES];
        }

    }
    else
    {
        UIAlertView *tAlertView=[[UIAlertView alloc] initWithTitle:@"Incorrect Pattern" message:@"You Have Entered wrong Password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Retry", nil];
        tAlertView.tag = 2;
        [tAlertView show];
    }
    
}


- (IBAction) enterButton:(UIButton *)pButton
{
	HomeViewController_iPad *tController = [[HomeViewController_iPad alloc] initWithNibName:@"HomeViewController_iPad" bundle:nil];
	[self.navigationController pushViewController:tController animated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return UIInterfaceOrientationIsLandscape(interfaceOrientation)? YES : NO;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}




@end
